var phone = null;
var newpassword = null;
var validcod = null;
var saveBtn = null;

$(function(){
	phone = $("#phone");
	newpassword = $("#newpassword");
	validcode = $("#validcode");
	saveBtn = $("#save");
	
	phone.next().html("*");
	newpassword.next().html("*");
	validcode.next().next().html("*");
	
	phone.on("blur", function() {
		var phone_reg = /^1(3|4|5|7|8)\d{9}$/;
		if (!phone_reg.test(phone.val())) {
			validateTip(phone.next(), {
				"color" : "red"
			}, imgNo + " 手机号码输入不符合规范，请重新输入", false);
		} else {
			validateTip(phone.next().next(), {
				"color" : "green"
			}, imgYes, true);
			//验证手机号是否已注册
			$.ajax({
				type : "POST",
				url : "/user/check_phone.do",
				data : {
					phone : phone.val()
				},
				dataType : "json",
				success : function(data) {
					if (data.status == 0) { //手机号码存在
						validateTip(phone.next(), {
							"color" : "green"
						}, imgYes + "号码已注册", true);
						$('input.sendidcode').removeAttr('disabled');
					} else if (data.status == 1) { //号码不存在
						$('input.sendidcode').attr('disabled', "true");
						validateTip(phone.next(), {
							"color" : "red"
						}, imgNo + "号码未注册", false);
					}
				},
				error : function(data) {
					//请求出错
					validateTip(phone.next(), {
						"color" : "red"
					}, imgNo + " 请求错误", false);
				}
			});
		}


	}).on("focus", function() {
		validateTip(phone.next(), {
			"color" : "#666666"
		}, "* 请输入手机号码", false);
	});
	
	
	
	newpassword.on("focus",function(){
		validateTip(newpassword.next(),{"color":"#666666"},"* 密码长度必须是大于6小于20",false);
	}).on("blur",function(){
		if(newpassword.val() != null && newpassword.val().length > 6
				&& newpassword.val().length < 20 ){
			validateTip(newpassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(newpassword.next(),{"color":"red"},imgNo + " 密码输入不符合规范，请重新输入",false);
		}
	});
	

	//判断验证码是否正确
		validcode.on("focus", function() {
			validateTip(validcode.next().next(), {
				"color" : "red"
			}, " * ", false);
		}).on("blur", function() {
			if (validcode.val() != null && validcode.val().length > 5
				&& validcode.val().length < 7) {
				//判断是否匹配
				$.post("/user/doCheckCode.do", {
					validCode : validcode.val(),
				}, function(data) {
					if (data.status == 0) {
						validateTip(validcode.next().next(), {
							"color" : "green"
						}, imgYes, true);
					} else {
						validateTip(validcode.next().next(), {
							"color" : "red"
						}, imgNo + "验证不匹配", false);
					}
				}, 'json');
			} else {
				validateTip(validcode.next().next(), {
					"color" : "red"
				}, "*", false);
			}
		});
	
	
	saveBtn.on("click",function(){
		phone.blur();
		newpassword.blur();
		validcode.blur();
		if(phone.attr("validateStatus") == "true"
			&& newpassword.attr("validateStatus") == "true"
			){
			
		if(confirm("确定要修改密码？")){
			
		$.ajax({
			type:"POST",
			url:"/user/forget_reset_password.do",
			data:{phone : phone.val(),
                  newpassword : newpassword.val(),
                  validcode : validcode.val(), 
                  },
			dataType:"json",			
			success:function(data){
			 //成功就跳转至登录页面，重新登录
			validcode.attr("validateStatus") == "true"
			window.location.href="to_tour_login";
			},
			error:function(data){
				//请求出错
				validateTip(newpassword.next(),{"color":"red"},imgNo + " 请求错误",false);
			}
		});
		}
		}
		
	});
});